#include <iostream>
#include <fstream>
#include "chontachs.pb.h"

void addPeopleInfo(chontachs1::PeopleInfo *people)
{
	std::cout << "-------------新增联系人------------" << std::endl;
	std::cout << "请输入联系人姓名：";
	std::string name;
	getline(std::cin, name);
	people->set_name(name);
	std::cout << "请输入联系人年龄：";
	int age;
	std::cin >> age;
	people->set_age(age);
	std::cin.ignore(256, '\n'); // 如果不清理，缓冲区中还有 \n 后续getline(std::cin,number);就直接把 \n 输入给number，就触发了break
	for (int i = 1;; i++)
	{
		std::cout << "请输入联系人电话" << i << "(按回车就退出)：";
		std::string number;
		getline(std::cin, number);
		if (number.empty())
		{
			break;
		}
		chontachs1::PeopleInfo_Phone *phone = people->add_phone();
		phone->set_number(number);
		std::cout << "电话类型：1.移动电话 2.固定电话: ";
		int type;
		std::cin >> type;
		std::cin.ignore(256, '\n');
		switch (type)
		{
		case 1:
			phone->set_type(chontachs1::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
			break;
		case 2:
			phone->set_type(chontachs1::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
			break;
		default:
			std::cout << "非法选择，使用默认值！" << std::endl;
			break;
		}
	}
	std::cout << "请输入联系人家庭地址：";
	std::string home_address;
	getline(std::cin, home_address);
	std::cout << "请输入联系人公司地址：";
	std::string unit_address;
	getline(std::cin, unit_address);
	chontachs1::Address address;
	address.set_home_address(home_address);
	address.set_unit_address(unit_address);

	google::protobuf::Any *data = people->mutable_data();
	data->PackFrom(address);

	std::cout << "请选择添加一个其他联系方式 (1、qq号 2、微信号) : ";
	int other_contact;
	std::cin >> other_contact;
	std::cin.ignore(254, '\n');
	if (1 == other_contact)
	{
		std::cout << "请输入qq号: ";
		std::string qq;
		getline(std::cin, qq);
		people->set_qq(qq);
	}
	else if (2 == other_contact)
	{
		std::cout << "请输入微信号: ";
		std::string wechat;
		getline(std::cin, wechat);
		people->set_wechat(wechat);
	}
	else
	{
		std::cout << "非法选择" << std::endl;
	}

	for (int i = 0;; i++)
	{
		std::cout << "请输入备注" << i << "标题 (只输入回车完成备注新增): ";
		std::string remark_key;
		getline(std::cin, remark_key);
		if (remark_key.empty())
		{
			break;
		}
		std::cout << "请输入备注" << i << "内容: ";
		std::string remark_value;
		getline(std::cin, remark_value);
		people->mutable_remark()->insert({remark_key,remark_value});
	}

	std::cout << "-----------新增联系人成功----------" << std::endl;
}

int main()
{
	chontachs1::Contacts contach;
	// 如果本地有信息先读取本地信息进行反序列化
	std::fstream input("contach.bin", std::ios::in | std::ios::binary);
	if (!input.is_open())
	{
		std::cout << " input open error , create contach.bin" << std::endl;
	}
	else if (!contach.ParseFromIstream(&input))
	{
		std::cout << " Parse error " << std::endl;
		input.close();
		return -1;
	}
	// 添加联系人
	addPeopleInfo(contach.add_contacts());
	// 把联系人写入本地信息

	std::fstream output("contach.bin", std::ios::out | std::ios::trunc | std::ios::binary);
	if (!contach.SerializeToOstream(&output))
	{
		std::cout << " serialize error " << std::endl;
		input.close();
		return -1;
	}

	std::cout << "write succeed" << std::endl;
	input.close();
	output.close();
	return 0;
}